//
//  CGApplyServiceViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/26.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGApplyServiceViewController.h"

@interface CGApplyServiceViewController ()

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)CGInputTextView *nameText;          //姓名
@property(nonatomic,strong)CGInputTextView *schoolText;        //大学
@property(nonatomic,strong)CGInputTextView *studentText;       //学号
@property(nonatomic,strong)CGInputTextView *qqText;            //QQ
@property(nonatomic,strong)CGInputTextView *idCardText;        //身份证
@property(nonatomic,strong)UIButton *submitButton;             //提交

@property(nonatomic,strong)UIWindow *coverWindow;
@property(nonatomic,strong)UIView *shadeView;
@property(nonatomic,strong)UIActivityIndicatorView *activity;
@property(nonatomic,strong)UILabel *indicateLabel;

@end

@implementation CGApplyServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.title = @"申请档案服务";
    
    [self layoutUI];
}

-(void)layoutUI{
    
    //底部添加一个scrollowView {{0, 0}, {320, 480}}
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 480-64-100)];
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 316);
    //self.scrollView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.scrollView];
    
    CGAccountDefault *user = [CGAccountDefault shareAccount];
    //姓名View
    CGRect aFrame = CGRectMake(0, 20, SCREEN_WIDTH, 44);
    self.nameText = [[CGInputTextView alloc]initWithFrame:aFrame];
    self.nameText.label.text = @"姓名";
    self.nameText.textField.placeholder = @"请输入姓名";
    self.nameText.textField.delegate = self;
    self.nameText.textField.text = user.name;
    [self.scrollView addSubview:self.nameText];
    
    //大学View
    aFrame.origin.y += 45;
    self.schoolText = [[CGInputTextView alloc]initWithFrame:aFrame];
    self.schoolText.label.text = @"大学";
    self.schoolText.textField.placeholder = @"请输入学校名称";
    self.schoolText.textField.delegate = self;
    self.schoolText.textField.text = user.schoolName;
    [self.scrollView addSubview:self.schoolText];
    
    //学号View
    aFrame.origin.y += 45;
    self.studentText = [[CGInputTextView alloc]initWithFrame:aFrame];
    self.studentText.label.text = @"学号";
    self.studentText.textField.placeholder = @"请输入学号";
    self.studentText.textField.delegate = self;
    self.studentText.textField.text = user.studentId;
    [self.scrollView addSubview:self.studentText];
    
    //QQ号View
    aFrame.origin.y += 45;
    self.qqText = [[CGInputTextView alloc]initWithFrame:aFrame];
    self.qqText.label.text = @"QQ";
    self.qqText.textField.placeholder = @"请输入QQ号";
    self.qqText.textField.delegate = self;
    self.qqText.textField.text = user.qq;
    [self.scrollView addSubview:self.qqText];
    
    //身份证View
    aFrame.origin.y += 45;
    self.idCardText = [[CGInputTextView alloc]initWithFrame:aFrame];
    self.idCardText.label.text = @"身份证";
    self.idCardText.textField.placeholder = @"请输入身份证";
    self.idCardText.textField.delegate = self;
    self.idCardText.textField.text = user.idCard;
    [self.scrollView addSubview:self.idCardText];
    
    //提交Button
    aFrame.origin.y += 70;
    aFrame.origin.x = 20;
    aFrame.size.width = SCREEN_WIDTH - 40;
    aFrame.size.height = 44;
    self.submitButton = [[UIButton alloc]initWithFrame:aFrame];
    [self.submitButton setBackgroundImage:[UIImage imageNamed:@"mine_btn_bg"] forState:UIControlStateNormal];
    [self.submitButton setTitle:@"提交" forState:UIControlStateNormal];
    self.submitButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.scrollView addSubview:self.submitButton];
    //添加点击事件
    [self.submitButton addTarget:self action:@selector(tapSubmitButton) forControlEvents:UIControlEventTouchUpInside];
    
}


#pragma mark 键盘弹出/退回事件
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.view endEditing:YES];
    return YES;
}


#pragma mark 提交按钮点击事件
-(void)tapSubmitButton{
    
    //1.有效性验证,去除字符串两端的空格
    NSString *name = [self.nameText.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *schoolName = [self.schoolText.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *studentId = [self.studentText.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *qq = [self.qqText.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *idCard = [self.idCardText.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (name.length == 0 || schoolName.length == 0 || studentId.length == 0 || qq.length == 0 || idCard.length == 0) {
        [Tools alertWithTitle:@"温馨提示" andMessage:@"请填写完整后再提交"];
        return;
    }
    
    [self popShadeView];   //开始转圈
    
    //发送提交请求
    CGAccountDefault *user = [CGAccountDefault shareAccount];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"account"] = user.account;
    parameters[@"password"] = user.password;
    parameters[@"tphone"] = user.tphone;
    parameters[@"name"] = name;
    parameters[@"schoolName"] = schoolName;
    parameters[@"studentId"] = studentId;
    parameters[@"qq"] = qq;
    parameters[@"idCard"] = idCard;

    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/files/saveapply.app" parameters:parameters success:^(id responseData) {
        
        [self hidShadeView];   //停止转圈
        
        //字典转模型
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"result:%@",result);
        
        if ([result rangeOfString:@"account"].location != NSNotFound) {
            
            //保存数据到沙箱
            CGAccount *user = [CGAccount objectWithJSONData:responseData];
            CGAccountDefault *accountDefault = [CGAccountDefault shareAccount];
            accountDefault.name = user.name;
            accountDefault.schoolName = user.schoolName;
            accountDefault.studentId = user.studentId;
            accountDefault.qq = user.qq;
            accountDefault.idCard = user.idCard;
            [accountDefault saveToSandBox];
            
            [Tools alertWithTitle:@"恭喜" andMessage:@"档案申请成功!"];
            
        }else{
            
        [Tools alertWithTitle:@"温馨提示" andMessage:@"系统繁忙,请稍后再试"];
            
        }

        
    } failure:^(NSError *error) {
        
        [self hidShadeView];  //停止转圈
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
        
    }];
    
    
}


#pragma mark 弹出网络延时加载框
-(void)popShadeView{
    
    self.shadeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.shadeView.backgroundColor = [UIColor blackColor];
    self.shadeView.alpha = 0.3;
    self.coverWindow = [UIApplication sharedApplication].keyWindow;
    [self.coverWindow addSubview:self.shadeView];
    
    //添加一个等待进度轮
    self.activity = [[UIActivityIndicatorView alloc]init];
    self.activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    self.activity.color = [UIColor blueColor];
    CGPoint center = self.shadeView.center;
    self.activity.center = center;
    [self.activity startAnimating];
    [self.coverWindow addSubview:self.activity];
    
    //添加一个提示Label
    self.indicateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.activity.frame)+10, SCREEN_WIDTH, 44)];
    self.indicateLabel.textAlignment = NSTextAlignmentCenter;
    self.indicateLabel.font = [UIFont systemFontOfSize:14];
    self.indicateLabel.textColor = [UIColor blueColor];
    self.indicateLabel.text = @"正在提交...";
    [self.coverWindow addSubview:self.indicateLabel];
    
}

#pragma mark 取消网络延时加载框
-(void)hidShadeView{
    
    self.shadeView.hidden = YES;
    self.indicateLabel.hidden = YES;
    [self.activity stopAnimating];
    
}


#pragma mark 注册键盘通知
-(void)viewWillAppear:(BOOL)animated{
    /*
     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 253}}";
     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 227}, {320, 253}}";
     */
    [super viewWillAppear:animated];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(openKeyboard:) name:UIKeyboardDidShowNotification object:nil];
    [center addObserver:self selector:@selector(closeKeyboard:) name:UIKeyboardWillHideNotification object:nil];
    
}

//键盘弹起时
-(void)openKeyboard:(NSNotification *)notification{
    
    if (IS_IPHONE4) {
        //self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 529);
        self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 480);
    }else if (IS_IPHONE5) {
        self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 392);
        
    }
    
}

//键盘收起时
-(void)closeKeyboard:(NSNotification*)notification{
    
    self.scrollView.contentSize =  CGSizeMake(SCREEN_WIDTH, 316);
}


@end
